package com.example.administrator.printoutindemo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.bean.VOOutStorageList;
import com.example.administrator.printoutindemo.mvp.ui.activity.LingLiaoShenQingQingDanActivity;
import com.example.administrator.printoutindemo.mvp.ui.entity.OutStorageCheckItem;
import com.example.administrator.printoutindemo.services.Tools;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.util.ArrayList;
import java.util.List;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;

import static com.jess.arms.base.BaseConstants.RESULT_CLOSE;


public class StoreSubmitProject3Activity extends BaseActivity {

    private static final String WEBSERVICE_CONFIG = "TEXT";
    private static final int LOGINSUCCESS = 0;
    private static final int NETWORKERROR = 2;
    private static final int SYSERROR = 3;
    private TextView s_sub3_txt_cly = null;
    private TextView s_sub3_txt_shr = null;
    private TextView s_sub3_txt_jsr = null;
    private TextView s_sub3_txt_jsrPhone = null;
    private TextView s_sub3_txt_ckxm = null;// 出库项目
    private TextView s_sub3_txt_mdxm = null;// 目的项目
    private TextView s_sub3_txt_cksj = null;// 出库时间
    private TextView s_sub3_txt_dhdd = null;// 到货地点
    private TextView s_sub2_act_txt_title = null;// 出库单号
    private TextView s_sub3_autoLoginName = null;
    private TextView s_sub3_autoLoginPwd = null;
    private ListView store_sub3_listView_outStorage = null;
    private Button store_sub3_btn_submit = null;
    private List<OutStorageList> osll = null;
    private OutStorageList outStorageList = null;
    private SharedPreferences sPreferencces;
    private TextView btnRight;
    private String mProid; //项目的id
    private OutStorageCheckItem outStorageCheckItem;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_activity_submitproject_3);
        ToolbarUtils.initToolbarTitleBack(this, getString(R.string.ling_liao_shen_qing));
        btnRight = (TextView) findViewById(R.id.btnRight);
        btnRight.setText("提交");
        sPreferencces = getSharedPreferences(WEBSERVICE_CONFIG, 0);

        s_sub3_txt_cly = (TextView) findViewById(R.id.s_sub3_txt_cly);
        s_sub3_txt_shr = (TextView) findViewById(R.id.s_sub3_txt_shr);
        s_sub3_txt_jsr = (TextView) findViewById(R.id.s_sub3_txt_jsr);
        s_sub3_txt_jsrPhone = (TextView) findViewById(R.id.s_sub3_txt_jsrPhone);
        s_sub3_txt_ckxm = (TextView) findViewById(R.id.s_sub3_txt_ckxm);
        s_sub3_txt_mdxm = (TextView) findViewById(R.id.s_sub3_txt_mdxm);
        s_sub3_txt_cksj = (TextView) findViewById(R.id.s_sub3_txt_cksj);
        s_sub3_txt_dhdd = (TextView) findViewById(R.id.s_sub3_txt_dhdd);
        s_sub2_act_txt_title = (TextView) findViewById(R.id.s_sub2_act_txt_title);
        store_sub3_listView_outStorage = (ListView) findViewById(R.id.store_sub3_listView_outStorage);
        store_sub3_btn_submit = (Button) findViewById(R.id.store_sub3_btn_submit);
        s_sub3_autoLoginName = (TextView) findViewById(R.id.s_sub3_autoLoginName);
        s_sub3_autoLoginPwd = (TextView) findViewById(R.id.s_sub3_autoLoginPwd);

        osll = (List<OutStorageList>) getIntent().getSerializableExtra(
                "outStorageListInfo");
        outStorageList = new OutStorageList(this);

        if (null != osll && osll.size() > 0) {

            OutStorageList osl = osll.get(0);
            mProid = osl.get_proid();
//			s_sub3_txt_cly.setText(((dyjApplication) getApplication()).get_user_name());
//            s_sub3_txt_cly.setText(sPreferencces.getString(BaseConstants.USERNAME, ""));
            s_sub3_txt_cly.setText(osl.getShenQingRen());
            s_sub3_txt_shr.setText(osl.get_check_user_name());
            s_sub3_txt_jsr.setText(osl.get_receive_username());
            s_sub3_txt_jsrPhone.setText(osl.get_receive_userphone());
            s_sub3_txt_ckxm.setText(osl.get_projectName());
            s_sub3_txt_mdxm.setText(osl.get_target_project());
            s_sub3_txt_cksj.setText(osl.get_out_storage_estimate());
            s_sub3_txt_dhdd.setText(osl.get_receive_address());
            s_sub2_act_txt_title.setText(osl.get_out_storagelist_batchid());
//            s_sub3_autoLoginName.setText(osl.getMaterialCode());
//            s_sub3_autoLoginPwd.setText(osl.getUnit());

            StoreSubmitProject3Adapter adapter = new StoreSubmitProject3Adapter(
                    this);
            adapter.setList(osll);
            store_sub3_listView_outStorage.setAdapter(adapter);

        }

        btnRight.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String t = s_sub2_act_txt_title.getText().toString();

                if (!"".equals(t)) {
                    try {
                        insertIntoOutStorageListBatch(osll);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    Tools.showDialogInfo(StoreSubmitProject3Activity.this, store_sub3_btn_submit, "出库单不明确，请返回填写出库数量！");
                }


            }
        });
    }

    @SuppressLint("LongLogTag")
    private void insertIntoOutStorageListBatch(List<OutStorageList> li) {

        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);

        String jsonStr = null;
        List<VOOutStorageList> jsonLists = new ArrayList<>();
        //TODO json数组
        for (int i = 0; i < li.size(); i++) {
            OutStorageList n = li.get(i);
//            jsonStr = outStorageList.ConvertToJson(n);
            jsonLists.add(outStorageList.ConvertToJson1(n));
        }
        Gson g = new Gson();
        jsonStr = g.toJson(jsonLists);


        String methodName = "InsertIntoOutStorageListBatch";

        SoapParams params = new SoapParams();
        params.put("proid", mProid);
        params.put("modelJson", jsonStr);

        Log.e("dayincangshu==============",jsonStr);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {
                LoadingDialogUtil.dismissDialog();

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {

                    try {
                        Gson gson = new Gson();
                        outStorageCheckItem = gson.fromJson(result, OutStorageCheckItem.class);

                        ArmsUtils.makeText(StoreSubmitProject3Activity.this, "申请出库成功！");
                        Intent in = new Intent(StoreSubmitProject3Activity.this, LingLiaoShenQingQingDanActivity.class);
//                    in.setClass(StoreSubmitProject3Activity.this, StoreSubmitProject1Activity.class);
                        Bundle bunl = new Bundle();
                        bunl.putSerializable("outStorageCheckItem", outStorageCheckItem);
                        in.putExtras(bunl);
                        startActivity(in);
                        setResult(RESULT_CLOSE);
                        finish();
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                        ArmsUtils.makeText(StoreSubmitProject3Activity.this, result);
                    }
                } else {
                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
                LoadingDialogUtil.dismissDialog();
                ArmsUtils.makeText(getContext(), error.getMessage());

            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
                LoadingDialogUtil.dismissDialog();

            }
        }, getAapiUserName(), getApiPwd());

    }


}